﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WoobaoForms
{
    public partial class FrmUsuario : Form
    {
        public FrmUsuario()
        {
            InitializeComponent();
        }

        private void FrmUsuario_Load(object sender, EventArgs e)
        {
           
        }

        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            try
            {


                using (DBWoobaoEntities banco = new DBWoobaoEntities())
                {
                    TBUsuario a = new TBUsuario();

                    a.Nome = this.txtNome.Text;
                    a.Email = this.txtEmail.Text;
                    a.Senha = this.txtSenha.Text;
                    a.STAdministrador = this.chkAdministrador.Checked;
                    a.Time = this.txtTime.Text;

                    banco.TBUsuario.Add(a);
                    banco.SaveChanges();
                }

                MessageBox.Show("Cadastrado com sucesso!!");

                txtNome.Text = "";
                txtEmail.Text = "";
                txtSenha.Text = "";
                chkAdministrador.Checked = false;
                txtTime.Text = "";
            }
            catch (Exception ex)
            {

                MessageBox.Show("Erro:" + ex.Message);
            }

        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            
            using (DBWoobaoEntities banco = new DBWoobaoEntities())
            {
                //dgvUsuario.DataSource = banco.TBUsuario.ToList();                
                var list = from usu in banco.TBUsuario 
                           where
                           usu.Nome.Contains(txtNome.Text) 
                           && usu.Email.Contains (txtEmail.Text)
                           && usu.Time.Contains (txtTime.Text)
                          
                           select usu;
                dgvUsuario.DataSource = list.ToList();
                cboUsuario.DataSource = banco.TBUsuario.ToList(); 

            }             
        }

        private void cboUsuario_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void btnExcluir_Click(object sender, EventArgs e)    
         {
            using (DBWoobaoEntities banco = new DBWoobaoEntities())
            {
                TBUsuario obj = (TBUsuario)cboUsuario.SelectedValue;
                banco.TBUsuario.Attach(obj);
                banco.TBUsuario.Remove(obj);
                banco.SaveChanges();
                cboUsuario.DataSource = banco.TBUsuario.ToList(); 
               }
            MessageBox.Show("Usuário excluido com Sucesso!!");
        }
    }
}
